﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Xml;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NetCMS.Content;
using NetCMS.Content.Common;
using NetCMS.Model;
using NetCMS.Config;
using System.Net;

public partial class User_Login : NetCMS.Web.UI.BasePage
{
    user rot = new user();
    rootPublic pd = new rootPublic();
    public string SiteID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Logout();
            string TmUrl = Request.QueryString["urls"];
            if (TmUrl != null && TmUrl != "")
            {
                string tmDir = NetCMS.Config.UIConfig.dirUser.Trim() + "/index.aspx";
                if ((TmUrl.ToString().ToLower()).IndexOf(tmDir.ToLower()) == -1) { this.HidUrl.Value = TmUrl.ToString(); }
            }
            if (pd.getUserLoginCode() != 1) { safecodeTF.Visible = false; }
            else { safecodeTF.Visible = true; }
            SiteID = getSiteID();
        }
    }


    protected string getSiteID()
    {
        string _Str = "";
        string _dirdumm = NetCMS.Config.UIConfig.dirDumm;
        if (_dirdumm.Trim() != "")
        { _dirdumm = "/" + _dirdumm; }
        if (!File.Exists(Server.MapPath(_dirdumm + "/site.xml"))) { PageError("找不到配置文件(/site.xml).<li>请与系统管理员联系。</li>", ""); }
        string xmlPath = Server.MapPath(_dirdumm + "/site.xml");
        System.Xml.XmlDocument xdoc = new XmlDocument();
        xdoc.Load(xmlPath);
        XmlElement root = xdoc.DocumentElement;
        XmlNodeList elemList = root.GetElementsByTagName("siteid");
        _Str = elemList[0].InnerXml;
        return _Str;
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string UserName = this.TxtName.Text;
        string PassWord = this.TxtPassword.Text;
        if (UserName.Trim() == string.Empty || PassWord.Trim() == string.Empty)
        {
            PageError("请输入用户名和密码!", "login.aspx?" + Request.QueryString);
        }
        if (pd.getUserLoginCode() != 0)
        {
            string SafeCode = this.TxtVerifyCode.Text;
            if (Session["CheckCode"] == null)
            {
                PageError("验证码已过期,请返回重新登录!", "login.aspx?" + Request.QueryString);
            }
            string _SafeCode = Session["CheckCode"].ToString().ToUpper();
            Session.Remove("CheckCode");
            if (SafeCode != _SafeCode)
            {
                PageError("验证码输入不正确!", "login.aspx?" + Request.QueryString);
            }
        }
        GlobalUserInfo info;
        EnumLoginState state = Login(UserName, PassWord, out info);
        if (state == EnumLoginState.Succeed)
        {
            NetCMS.Global.Current.Set(info);
            if (Request.QueryString["reurl"] != null && Request.QueryString["reurl"] != string.Empty)
            {
                Response.Redirect(Request.QueryString["urls"], false);
            }
            else
            //{
            //    #region 整合Discuz!NT
            //    try
            //    {
            //        string xmlName = Server.MapPath("..\\api\\dz\\Adapt.config");
            //        AdaptConfig conf = new AdaptConfig(xmlName);
            //        if (conf.isAdapt)
            //        {
            //            string adaptePath = conf.adaptPath;
            //            adaptePath += "?username=" + UserName + "&password=" + PassWord + "&tag=login";
            //            //Response.Write("<script type=\"text/javascript\" language=\"javascript\">window.open(\"" + adaptePath + "\",\"\",\"left=5000,top=5000\");</script>");
            //            string str = "<script language=\"javascript\" type=\"text/javascript\" src=\"" + adaptePath + "\"></script>";                         
            //            Response.Write(str);                                               
            //        }
            //    }
            //    #endregion

            //finally
            {

                #region 动易，动网，Oblog 整合
                NetCMS.API.PDO.PDOAction pdo = new NetCMS.API.PDO.PDOAction();
                if (pdo.Enabled())
                {
                    pdo.Login(UserName, PassWord, 1);
                }
                #endregion

                Response.Write("<script language=\"javascript\">window.top.location.href=\"Index.aspx?urls=" + this.HidUrl.Value + "\";</script>");
                System.Web.HttpContext.Current.Response.End();
                //Response.End();
                //Response.Redirect("index.aspx?urls=" + this.HidUrl.Value, false);
                //}
            }
        }
        else
        {
            LoginResultShow(state);
        }
    }
}
